package top.xchch.workdemo.func01;

import java.util.concurrent.*;

/**
 * 线程同步方式一
 *  使用callable 和 future, 在主线程get拿到任务线程的返回值
 *
 * @author sai.zhang
 * @date 2021-10-17 19:29
 */
public class Main {

    private static ExecutorService es = Executors.newFixedThreadPool(4);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();

        Future<Integer> submit = es.submit(() -> {
            return new ThirdObj().task();
        });

        // 确保  拿到result 并输出
        System.out.println("异步计算结果为: " + submit.get());
        System.out.println("使用时间："+ (System.currentTimeMillis() - start) + " ms");

        es.shutdown();
    }
}

class ThirdObj {

    public Integer task() {
        return fibo(36);
    }

    private int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
